

#include "GUI.h""

#include "oled.h"
#include "stdio.h"
#include "stdlib.h"
#include "math.h"


void GUI_Init()
{
    OLED_Init();
}

void GUI_LineGif(void)
{
    static uint8_t x = 0;
    static uint8_t y = 0;
    static int8_t  x_off = 1;
    static int8_t  y_off = 1;

    OLED_Clear();

    DrawLine(0,0,x,GUI_HIGH-1);
    DrawLine(0,0,GUI_WIDTH-1,y);
    DrawLine(GUI_WIDTH-1,0,x,GUI_HIGH-1);
    DrawLine(GUI_WIDTH-1,0,0,GUI_HIGH-1-y);
    DrawLine(0,GUI_HIGH-1,GUI_WIDTH-1-x,0);
    DrawLine(0,GUI_HIGH-1,GUI_WIDTH-1,y);
    DrawLine(GUI_WIDTH-1,GUI_HIGH-1,GUI_WIDTH-1-x,0);
    DrawLine(GUI_WIDTH-1,GUI_HIGH-1,0,GUI_HIGH-1-y);

    x=x+x_off;
    y=y+y_off;


    if(x>=GUI_WIDTH-1)
    {
        x_off=-1;
    }
    else if (x==0)
    {
        x_off=1;
    }

    if(y>=GUI_HIGH-1)
    {
        y_off=-1;
    }
    else if (y==0)
    {
        y_off=1;
    }

}



void DrawCircle ( int usX_Center, int usY_Center, int usRadius)
{
	short sCurrentX, sCurrentY;
	short sError;
	sCurrentX = 0; sCurrentY = usRadius;	  
	sError = 3 - ( usRadius << 1 );     //判断下个点位置的标志
	while ( sCurrentX <= sCurrentY )
	{      
			OLED_DrawPoint ( usX_Center + sCurrentX, usY_Center + sCurrentY);             //1，研究对象
			OLED_DrawPoint ( usX_Center - sCurrentX, usY_Center + sCurrentY );             //2      
			OLED_DrawPoint ( usX_Center - sCurrentY, usY_Center + sCurrentX );             //3
			OLED_DrawPoint ( usX_Center - sCurrentY, usY_Center - sCurrentX );             //4
			OLED_DrawPoint ( usX_Center - sCurrentX, usY_Center - sCurrentY );             //5       
			OLED_DrawPoint ( usX_Center + sCurrentX, usY_Center - sCurrentY );             //6
			OLED_DrawPoint ( usX_Center + sCurrentY, usY_Center - sCurrentX );             //7 
			OLED_DrawPoint ( usX_Center + sCurrentY, usY_Center + sCurrentX );             //0
			sCurrentX ++;		
			if ( sError < 0 ) 
				sError += 4 * sCurrentX + 6;	  
			else
			{
				sError += 10 + 4 * ( sCurrentX - sCurrentY );   
				sCurrentY --;
			} 		
	}
}





void GUI_RegGif(void)
{
    ;
}

void DrawLine(int x1,int y1,int x2,int y2)
{
	unsigned short us; 
	unsigned short usX_Current, usY_Current;
	
	int lError_X = 0, lError_Y = 0, lDelta_X, lDelta_Y, lDistance; 
	int lIncrease_X, lIncrease_Y; 	
	
	lDelta_X = x2 - x1; //计算坐标增量 
	lDelta_Y = y2 - y1; 
	
	usX_Current = x1; 
	usY_Current = y1; 
	
	
	if ( lDelta_X > 0 ) 
		lIncrease_X = 1; //设置单步方向 
	
	else if ( lDelta_X == 0 ) 
		lIncrease_X = 0;//垂直线 
	
	else 
  { 
    lIncrease_X = -1;
    lDelta_X = - lDelta_X;
  } 

	
	if ( lDelta_Y > 0 )
		lIncrease_Y = 1; 
	
	else if ( lDelta_Y == 0 )
		lIncrease_Y = 0;//水平线 
	
	else 
  {
    lIncrease_Y = -1;
    lDelta_Y = - lDelta_Y;
  } 

	
	if (  lDelta_X > lDelta_Y )
		lDistance = lDelta_X; //选取基本增量坐标轴 
	
	else 
		lDistance = lDelta_Y; 
	
	for ( us = 0; us <= lDistance + 1; us ++ )//画线输出 
	{  	
        OLED_DrawPoint(usX_Current,usY_Current);
		lError_X += lDelta_X ; 
		lError_Y += lDelta_Y ; 
		
		if ( lError_X > lDistance ) 
		{ 
			lError_X -= lDistance; 
			usX_Current += lIncrease_X; 
		}  
		
		if ( lError_Y > lDistance ) 
		{ 
			lError_Y -= lDistance; 
			usY_Current += lIncrease_Y; 
		} 		
	}  
}

void DrawFastHLine(int x, int y, unsigned char w)
{
  int end = x+w;
	int a;
  for ( a = MAX(0,x); a < MIN(end,SCREEN_COLUMN); a++)
  {
    OLED_DrawPoint(a,y);
  }
}
void DrawFastVLine(int x, int y, unsigned char h)
{
  int end = y+h;
	int a;
  for (a = MAX(0,y); a < MIN(end,SCREEN_ROW); a++)
  {
    OLED_DrawPoint(x,a);
  }
}



void DrawRect1(int left,int top,int right,int bottom)
{
		DrawLine ( left, top, right, top );
		DrawLine ( left, bottom , right , bottom );
		DrawLine ( left, top, left, bottom );
		DrawLine ( right , top, right , bottom );	
}

void DrawFillRect1(int left,int top,int right,int bottom)
{

		DrawRect1(left,top,right,bottom);
		FillRect(left+1,top+1,right-left-1,bottom-top-1);	
}

void DrawRect2(int left,int top,int width,int height)
{
		DrawLine ( left, top, left+width-1, top );
		DrawLine ( left, top+height-1 , left+width-1 , top+height-1 );
		DrawLine ( left, top, left, top+height-1);
		DrawLine ( left+width-1 , top, left+width-1 , top+height-1);	
}
void DrawFillRect2(int left,int top,int width,int height)
{
		DrawRect2(left,top,width,height);
		FillRect(left+1,top+1,width-1,height-1);	
}








void DrawCircleHelper(int x0, int y0, unsigned char r, unsigned char cornername)
{
  int f = 1 - r;
  int ddF_x = 1;
  int ddF_y = -2 * r;
  int x = 0;
  int y = r;
  while (x<y)
  {
    if (f >= 0)
    {
      y--;
      ddF_y += 2;
      f += ddF_y;
    }

    x++;
    ddF_x += 2;
    f += ddF_x;

    if (cornername & 0x4)
    {
      OLED_DrawPoint(x0 + x, y0 + y);
      OLED_DrawPoint(x0 + y, y0 + x);
    }
    if (cornername & 0x2)
    {
      OLED_DrawPoint(x0 + x, y0 - y);
      OLED_DrawPoint(x0 + y, y0 - x);
    }
    if (cornername & 0x8)
    {
      OLED_DrawPoint(x0 - y, y0 + x);
      OLED_DrawPoint(x0 - x, y0 + y);
    }
    if (cornername & 0x1)
    {
      OLED_DrawPoint(x0 - y, y0 - x);
      OLED_DrawPoint(x0 - x, y0 - y);
    }
  }
}
void DrawFillCircleHelper(int x0, int y0, unsigned char r, unsigned char cornername, int delta)
{
  // used to do circles and roundrects!
  int f = 1 - r;
  int ddF_x = 1;
  int ddF_y = -2 * r;
  int x = 0;
  int y = r;
  while (x < y)
  {
    if (f >= 0)
    {
      y--;
      ddF_y += 2;
      f += ddF_y;
    }

    x++;
    ddF_x += 2;
    f += ddF_x;

    if (cornername & 0x1)
    {
      DrawFastVLine(x0+x, y0-y, 2*y+1+delta);
      DrawFastVLine(x0+y, y0-x, 2*x+1+delta);
    }

    if (cornername & 0x2)
    {
      DrawFastVLine(x0-x, y0-y, 2*y+1+delta);
      DrawFastVLine(x0-y, y0-x, 2*x+1+delta);
    }
  }
}

void DrawFillCircle( int usX_Center, int usY_Center, int r)
{
    DrawFastVLine(usX_Center, usY_Center-r, 2*r+1);
    DrawFillCircleHelper(usX_Center,usY_Center, r, 3, 0);
}



void LineTo(uint8_t x,uint8_t y)
{
  static uint8_t last_x = 0;
  static uint8_t last_y = 0;
	DrawLine(last_x,last_y,x,y);
	last_x=x;
	last_y=y;

}


